package com.seh.contentcenter.service.Impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seh.contentcenter.dto.ChatDTO;
import com.seh.contentcenter.entity.Chat;
import com.seh.contentcenter.feignclient.UserCenterFeignClient;
import com.seh.contentcenter.mapper.ChatMapper;
import com.seh.contentcenter.service.ChatService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ChatServiceImpl implements ChatService {
    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private RedisTemplate<String, Object> objectRedisTemplate;



    /**
     * 新增消息记录
     *
     * @param chatDTO
     */
    @Override
    @CachePut(value = "chatRecord", key = "#chatDTO.fromId + '-' + #chatDTO.toId")
    public void saveChat(ChatDTO chatDTO) {
        Chat chat = new Chat();
        BeanUtils.copyProperties(chatDTO, chat);
        chatMapper.insert(chat);
    }

    /**
     * 查询消息记录
     *
     * @param fromId
     * @param toId
     * @return
     */
    @Cacheable(value = "chat", key = "#fromId + '-' + #toId")
    public List<Chat> getChatRecord(Integer fromId, Integer toId) {
        QueryWrapper<Chat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fromId", fromId).eq("toId", toId)
                .orderByAsc("send_time");
        return chatMapper.selectList(queryWrapper);
    }
}
